package com.zzptc.liantian_android_1;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.zzptc.liantian_android_1.adapter.Jiemian_adapter;
import com.zzptc.liantian_android_1.vo.AddXianshi;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    TextView tv_xianshi;
    EditText et_bianjikuang;
    Button btn_fasong;
    List<AddXianshi> list=new ArrayList<AddXianshi>();

    Jiemian_adapter jiemian_adapter;
    String srg;
    RecyclerView rvXianshi;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        et_bianjikuang = (EditText) findViewById(R.id.et_bianjikuang);
        btn_fasong = (Button) findViewById(R.id.btn_fashong);
        rvXianshi= (RecyclerView) findViewById(R.id.rv_xianshi);

    }

    public void btn_fashong(View view) {
        final String et_bianjikuang_1 = et_bianjikuang.getText().toString().trim();
        et_bianjikuang.setText("");



        new AsyncTask<String, View, String>() {
            @Override
            protected String doInBackground(String... strings) {
                try {
                    URL url = new URL("http://i.itpk.cn/api.php?question=" + et_bianjikuang_1);
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    connection.setRequestMethod("GET");
                    if (connection.getResponseCode() == 200) {
                        InputStream is = connection.getInputStream();
                        BufferedReader bfr = new BufferedReader(new InputStreamReader(is));
                        srg = bfr.readLine();
                        bfr.close();
                        is.close();
                    }
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }

                return srg;
            }

            @Override
            protected void onPostExecute(String s) {
                super.onPostExecute(s);
                AddXianshi addXianshi = new AddXianshi();
                addXianshi.setXianshi(s);
                addXianshi.setTvxiaoshi(et_bianjikuang_1);
                list.add(addXianshi);

                LinearLayoutManager linearLayoutManager = new LinearLayoutManager(MainActivity.this);
                rvXianshi.setLayoutManager(linearLayoutManager);

                jiemian_adapter = new Jiemian_adapter(list);
                rvXianshi.setAdapter(jiemian_adapter);



            }
        }.execute();


    }


}
